/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ResultEditor.AnnotationClasses;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;

/**
 *
 * @author leng
 */
/**Tree node renderer for annotated classes in tree view.*/
public class Treeview_NodeRenderer implements TreeCellRenderer {
    private JClassCheckBox classRenderer_checkbox = new JClassCheckBox();
    private JAnnotationCheckBox annotationRendere_checkbox = new JAnnotationCheckBox();
    private Component return_component;
    //private JList annotatedclassRenderer_list = new JList();

    private DefaultTreeCellRenderer normalRenderer = new DefaultTreeCellRenderer();

    Color selectionBorderColor, selectionForeground, selectionBackground,
        textForeground, textBackground;

    protected Object getClassRenderer() {
        return this.return_component;
    }

    public Treeview_NodeRenderer() {
        super();
        classRenderer_checkbox.setIconTextGap(30);


        Font fontValue;
        //fontValue = UIManager.getFont("Arial");
        //if (fontValue != null) {
            //classRenderer_checkbox.setFont(fontValue);
            classRenderer_checkbox.setFont( new Font("Calibri", Font.BOLD, 14 ) );
            annotationRendere_checkbox.setFont( new Font("Calibri", Font.BOLD, 14 ) );
        //}
        Boolean booleanValue = (Boolean) UIManager.get("Tree.drawsFocusBorderAroundIcon");
        classRenderer_checkbox.setFocusPainted((booleanValue != null) && (booleanValue.booleanValue()));
        //annotationRendere_checkbox.setFocusPainted((booleanValue != null) && (booleanValue.booleanValue()));

        selectionBorderColor = UIManager.getColor("Tree.selectionBorderColor");
        selectionForeground = UIManager.getColor("Tree.selectionForeground");
        selectionBackground = UIManager.getColor("Tree.selectionBackground");
        textForeground = UIManager.getColor("Tree.textForeground");
        textBackground = UIManager.getColor("Tree.textBackground");

    }

    public Component getTreeCellRendererComponent(JTree tree, Object value,
        boolean selected, boolean expanded, boolean leaf, int row,
        boolean hasFocus) {

        //Component returnValue;

        // ##1## to node of class/markable
        if ( (value != null) && (value instanceof Treeview_ClassNode) ) {

            if ( selected ) {                
                classRenderer_checkbox.setForeground(selectionForeground);
                classRenderer_checkbox.setBackground(selectionBackground);
            } else {
                classRenderer_checkbox.setForeground(textForeground);
                classRenderer_checkbox.setBackground(textBackground);
            }



            String stringValue = tree.convertValueToText(value, selected, expanded, leaf, row, false);
            classRenderer_checkbox.setText(stringValue);
            classRenderer_checkbox.setSelected(false);

            classRenderer_checkbox.setEnabled(tree.isEnabled());
            

            //if ( (value != null) && (value instanceof Treeview_ClassNode) ) {
            Color c = ((Treeview_ClassNode)value).getClassColor();
            classRenderer_checkbox.setClassColor(c);
            classRenderer_checkbox.setText(((Treeview_ClassNode)value).getText());
            classRenderer_checkbox.setSelected(((Treeview_ClassNode)value).isSelected());
            classRenderer_checkbox.setText( ((Treeview_ClassNode)value).getDisplayText() );
            classRenderer_checkbox.setAnnotationAmount( ((Treeview_ClassNode)value).getAnnotationAmount() );
            classRenderer_checkbox.setAnnotationTypeAmount( ((Treeview_ClassNode)value).getAnnotationTypeAmount() );
            classRenderer_checkbox.setClassname( ((Treeview_ClassNode)value).text );
            classRenderer_checkbox.setflag_isSelected_toCreateAnnotation( ((Treeview_ClassNode)value).isSelected_toCreateAnnotation );


            Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
            if (userObject instanceof Treeview_ClassNode) {
                Treeview_ClassNode node = (Treeview_ClassNode) userObject;
                classRenderer_checkbox.setText(node.getDisplayText());
                classRenderer_checkbox.setSelected( node.isSelected() );               
                //classRenderer_checkbox.setflag_isSelected_toCreateAnnotation( node.isSelected_toCreateAnnotation );
            }

            this.return_component = classRenderer_checkbox;
            return this.return_component;

        }

        if ( (value != null) && (value instanceof Treeview_AnnotationNode) ) {
            //System.out.println("hello");
            //JAnnotationCheckBox annotationCheckBox = new JAnnotationCheckBox();

            // build the annotation diamond icon
            Color color = ((Treeview_AnnotationNode)value).getClassColor();
            annotationRendere_checkbox.iconcolor = color;
            if(((Treeview_AnnotationNode)value).isNotAppeared_in_CurrentArticle)
                color = Color.BLACK;
            DiamondIcon icon = new DiamondIcon( color );
            annotationRendere_checkbox.setIcon(icon);

            String text = ((Treeview_AnnotationNode)value).getText();
            annotationRendere_checkbox.setText( text );
            annotationRendere_checkbox.setOpaque(true);

            //System.out.println("selected = " + selected);         
            if ( selected ) {
                annotationRendere_checkbox.setForeground(selectionForeground);
                annotationRendere_checkbox.setBackground(selectionBackground);
            } else {
                annotationRendere_checkbox.setForeground(textForeground);
                annotationRendere_checkbox.setBackground(textBackground);
            }

            // Get the length of text showed on the checkbox and set the size of
            // this check box.
            Graphics2D g2d = (Graphics2D) annotationRendere_checkbox.getGraphics() ;
            if ( g2d != null) {
                g2d.setFont( new Font("Calibri", Font.BOLD, 14 ) );
                String annotationText = ((Treeview_AnnotationNode)value).getUniqueAnnotationText();
                int fontcharwidth = g2d.getFontMetrics().stringWidth( annotationText );
                String annotationAmountString =  String.valueOf(((Treeview_AnnotationNode)value).getUniqueAnnotationAmount());
                fontcharwidth = fontcharwidth + g2d.getFontMetrics().stringWidth( annotationAmountString + " " + "[]" );
                fontcharwidth = fontcharwidth + 60;
                //annotationRendere_checkbox.setMinimumSize( new Dimension(fontcharwidth,22));
                annotationRendere_checkbox.setPreferredSize( new Dimension(fontcharwidth,22));
                //annotationRendere_checkbox.setSize( 0,22);

            }

            if( ( ((Treeview_AnnotationNode)value).isHighLighted  )||(selected)){
                annotationRendere_checkbox.setForeground(selectionForeground);
                annotationRendere_checkbox.setBackground(selectionBackground);
            }else{
                annotationRendere_checkbox.setForeground(textForeground);
                annotationRendere_checkbox.setBackground(textBackground);
            }

            annotationRendere_checkbox.setUniqueAnnotationText( ((Treeview_AnnotationNode)value).getText() );
            annotationRendere_checkbox.setUniqueAnnotationAmount( ((Treeview_AnnotationNode)value).UniqueAunotationAmount );

            Object userObject = ((DefaultMutableTreeNode) value).getUserObject();

            if (userObject instanceof Treeview_AnnotationNode) {
                Treeview_AnnotationNode anode = (Treeview_AnnotationNode) userObject;
                annotationRendere_checkbox.setUniqueAnnotationText( anode.getText() );
                annotationRendere_checkbox.setUniqueAnnotationAmount( anode.UniqueAunotationAmount );

                //boolean isselected = true;

                annotationRendere_checkbox.setOpaque(true);
                //if ( isselected ) {
                //    annotationRendere_checkbox.setForeground(selectionForeground);
                //    annotationRendere_checkbox.setBackground(selectionBackground);
                //} else {
                //    annotationRendere_checkbox.setForeground(textForeground);
               //     annotationRendere_checkbox.setBackground(textBackground);
               // }


                annotationRendere_checkbox.isNotAppeared_in_CurrentArticle = anode.isNotAppeared_in_CurrentArticle;
                //annotationRendere_checkbox.setSelected(anode.isSelected());

                annotationRendere_checkbox.setText( anode.getText() );
            }

            //returnValue = annotationCheckBox;
            //annotatedclassRendere_jlabel = annotationCheckBox;
            this.return_component = annotationRendere_checkbox;
            return this.return_component;

        }else {
            this.return_component = normalRenderer.getTreeCellRendererComponent(tree,
            value, selected, expanded, leaf, row, hasFocus);
            //System.out.println("error 1008260042 - returned wrong type of component for treeview cell!!!");
        }

        return this.return_component;
    }
}